package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;

import java.util.ArrayList;
import java.util.List;


public class MenuTypeItemAdapter extends BaseAdapter {

    private static final String TAG = "MenuTypeItemAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<ItemInfo> data;

    public MenuTypeItemAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    public List<String> getData() {
        List<String> temp = new ArrayList<>();
        for (ItemInfo itemInfo : data) {
            temp.add(itemInfo.getName());
        }
        return temp;
    }


    public void setItems(List<String> data) {
        if (data.isEmpty()) {
            return;
        }
        this.data.clear();
        for (String name : data) {
            this.data.add(new ItemInfo(name, false));
        }
        notifyDataSetChanged();
    }


    public void clearData() {
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Integer getItem(int position) {
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_io_1_item, null);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_name.setText(data.get(position).name);
        resetBg(convertView, position);
        return convertView;
    }

    public void setOnItemBg(String name) {
        List<ItemInfo> temp = new ArrayList<>();
        for (ItemInfo itemInfo1 : this.data) {
            if (TextUtils.equals(itemInfo1.getName(), name)) {
                temp.add(new ItemInfo(itemInfo1.getName(), true));
            } else {
                temp.add(new ItemInfo(itemInfo1.getName(), false));
            }

        }
        this.data.clear();
        this.data.addAll(temp);
        notifyDataSetChanged();
    }

    private void resetBg(View view, int position) {
        if (data.get(position).isBg()) {
            view.setBackgroundColor(Color.parseColor("#0000ff"));
        } else {
            view.setBackgroundColor(Color.parseColor("#eeeeee"));
        }
    }

    static class ViewHolder {
        TextView tv_name;
    }


    class ItemInfo {
        private String name;
        private boolean bg;

        public ItemInfo(String name, boolean bg) {
            this.name = name;
            this.bg = bg;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isBg() {
            return bg;
        }

        public void setBg(boolean bg) {
            this.bg = bg;
        }
    }

}